package xin.codedream.codelog.web.controller.admin;

import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import xin.codedream.codelog.exception.CategoryException;
import xin.codedream.codelog.model.entity.Category;
import xin.codedream.codelog.model.entity.Logs;
import xin.codedream.codelog.model.enums.LogsEnum;
import xin.codedream.codelog.model.enums.ResponseEnum;
import xin.codedream.codelog.model.global.ResponseData;
import xin.codedream.codelog.service.CategoryService;
import xin.codedream.codelog.service.LogsService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 分类目录控制层
 *
 * @author NGLSL
 * @date 2018/8/12
 */
@Slf4j
@RestController
@RequestMapping("/admin/cate")
public class CategoryController {
    @Resource
    private CategoryService categoryService;
    @Resource
    private LogsService logsService;
    @Resource
    private HttpServletRequest request;

    @PostMapping("/cateAll")
    public ResponseData findAllCategory() {
        ResponseData responseData = new ResponseData(ResponseEnum.SUCCESS_CODE);
        Map<String, Object> data = new HashMap<>(1);
        data.put("categoryList", categoryService.findAllCategory());
        responseData.setData(data);
        return responseData;
    }

    @GetMapping("/showCateAll")
    public ModelAndView showCateAll() {
        return new ModelAndView("admin/admin_category");
    }


    /**
     * 删除目录
     *
     * @param cateId 目录ID
     * @return ResponseData
     */
    @PostMapping("/deleteCategory")
    public ResponseData deleteCategory(Long cateId) {
        ResponseData responseData = new ResponseData();
        try {
            boolean result = categoryService.removeCategory(cateId);
            if (result) {
                responseData.setCodeMessage(ResponseEnum.DELETE_CATEGORY_SUCCESS);
                logsService.saveLogInfo(new Logs(LogsEnum.DELETE_CATEGORY.getMessage(), "目录ID：[" + cateId + "]" + ResponseEnum.DELETE_CATEGORY_SUCCESS.getMessage()
                        , ServletUtil.getClientIP(request)));
            } else {
                responseData.setCodeMessage(ResponseEnum.DELETE_CATEGORY_ERROR);
                logsService.saveLogInfo(new Logs(LogsEnum.DELETE_CATEGORY.getMessage(), "目录ID：[" + cateId + "]" + ResponseEnum.DELETE_CATEGORY_ERROR.getMessage()
                        , ServletUtil.getClientIP(request)));
            }
        } catch (CategoryException e) {
            log.error("目录删除异常：", e);
            responseData.setCodeMessage(ResponseEnum.DELETE_CATEGORY_ERROR);
            logsService.saveLogInfo(new Logs(LogsEnum.DELETE_CATEGORY.getMessage(), "目录ID：[" + cateId + "]" + ResponseEnum.DELETE_CATEGORY_ERROR.getMessage()
                    , ServletUtil.getClientIP(request)));
        }
        return responseData;
    }

    /**
     * 查看目录详情
     *
     * @param cateId 目录ID
     * @param cateType   类型
     * @return ModelAndView
     */
    @GetMapping("/detail")
    public ModelAndView showCategoryInfo(Long cateId, String cateType) {
        ModelAndView mv = new ModelAndView("/admin/admin_category_edit");
        mv.addObject("cateType", cateType);
        if (cateId != null) {
            Category category = categoryService.findCategoryByCateId(cateId);
            mv.addObject("category", category);
        }
        return mv;
    }

    /**
     * 更新目录信息
     *
     * @param category 目录信息
     * @return ResponseData
     */
    @PostMapping("/updateCategoryInfo")
    public ResponseData updateCategoryInfo(@ModelAttribute Category category) {
        ResponseData responseData = new ResponseData();
        try {
            boolean updateResult = categoryService.updateCategoryInfo(category);
            if (updateResult) {
                responseData.setCodeMessage(ResponseEnum.UPDATE_CATEGORY_SUCCESS);
                logsService.saveLogInfo(new Logs(LogsEnum.UPDATE_CATEGORY.getMessage(), "目录名称：[" + category.getCateName() + "]" + ResponseEnum.UPDATE_CATEGORY_SUCCESS.getMessage()
                        , ServletUtil.getClientIP(request)));
            } else {
                responseData.setCodeMessage(ResponseEnum.UPDATE_CATEGORY_SUCCESS);
                logsService.saveLogInfo(new Logs(LogsEnum.UPDATE_CATEGORY.getMessage(), "目录名称：[" + category.getCateName() + "]" + ResponseEnum.UPDATE_CATEGORY_SUCCESS.getMessage()
                        , ServletUtil.getClientIP(request)));
            }
        } catch (Exception e) {
            log.error("目录信息更新失败：", e);
            responseData.setCodeMessage(ResponseEnum.UPDATE_CATEGORY_SUCCESS);
            logsService.saveLogInfo(new Logs(LogsEnum.UPDATE_CATEGORY.getMessage(), "目录名称：[" + category.getCateName() + "]" + ResponseEnum.UPDATE_CATEGORY_SUCCESS.getMessage()
                    , ServletUtil.getClientIP(request)));
        }
        return responseData;
    }

    /**
     * 保存目录信息
     *
     * @param category 目录信息
     * @return ResponseData
     */
    @PostMapping("/saveCategoryInfo")
    public ResponseData saveCategoryInfo(@ModelAttribute Category category) {
        ResponseData responseData = new ResponseData();
        try {
            boolean saveResult = categoryService.saveCategoryInfo(category);
            if (saveResult) {
                responseData.setCodeMessage(ResponseEnum.SAVE_CATEGORY_SUCCESS);
                logsService.saveLogInfo(new Logs(LogsEnum.SAVE_CATEGORY.getMessage(), "目录名称：[" + category.getCateName() + "]" + ResponseEnum.SAVE_CATEGORY_SUCCESS.getMessage()
                        , ServletUtil.getClientIP(request)));
            } else {
                responseData.setCodeMessage(ResponseEnum.SAVE_CATEGORY_ERROR);
                logsService.saveLogInfo(new Logs(LogsEnum.SAVE_CATEGORY.getMessage(), "目录名称：[" + category.getCateName() + "]" + ResponseEnum.SAVE_CATEGORY_ERROR.getMessage()
                        , ServletUtil.getClientIP(request)));
            }
        } catch (Exception e) {
            log.error("保存目录信息错误：", e);
            responseData.setCodeMessage(ResponseEnum.SAVE_CATEGORY_ERROR);
            logsService.saveLogInfo(new Logs(LogsEnum.SAVE_CATEGORY.getMessage(), "目录名称：[" + category.getCateName() + "]" + ResponseEnum.SAVE_CATEGORY_ERROR.getMessage()
                    , ServletUtil.getClientIP(request)));
        }
        return responseData;
    }
}
